<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>友情链接管理</title>	
	</head>
	<body>
		<center>
			<!-- 实现商品增删改查 -->
			<h3>商品信息管理</h3>
			<?php
				require("../../public/config.php");  //导入数据库配置文件
				
				$link = @mysqli_connect(HOST,USER,PASS,DBNAME) or die("数据库连接失败！");	//连接数据库
				
				mysqli_set_charset($link,"utf8");		//设置数据库编码
				
				switch($_GET['a']){		//根据参数a的值进行相应的操作
					case "insert":
						//获取其他页面添加过来的信息
						$urlname = $_POST['urlname'];
						$links = $_POST['links'];

						if(empty($_POST['urlname'])){
							echo '<script>alert("链接名称不能为空！"); </script>';
							header("refresh:0;url=add.php");
							exit();
						}
						if(empty($_POST['links'])){
							echo '<script>alert("链接不能为空！"); </script>';
							header("refresh:0;url=add.php");
							exit();
						}


						//拼接sql语句
						$sql = "insert into links(urlname,links) values('{$urlname}','{$links}')";
						// echo $sql;
						//执行操作
						mysqli_query($link,$sql);
						//判断输出
						if(mysqli_insert_id($link)){
							echo '<script>alert("添加成功！"); </script>';
							header("refresh:0;url=index.php");
							exit();
						}else{
							echo "添加失败！".mysqli_error($link);
							header("refresh:3;url=index.php");
						}
						break;
					//执行删除操作
				  case "del":				
					   $id = $_GET['id'];
					   $sql = "delete from links where id=".$id;
					   mysqli_query($link,$sql);
					   if(mysqli_affected_rows($link)>0){
						   header("Location:".$_SERVER["HTTP_REFERER"]);//删除成功后跳转到浏览时的位置
						   exit();
					   }else{
						  	echo "删除失败！".mysqli_error($link);
							header("refresh:3;url=index.php");
					   }
					   break;
					   
				case "update"://数据修改

	                	$id = $_POST['id'];
	                	$urlname= $_POST['urlname'];
	                	$links= $_POST['links'];


						if(empty($_POST['urlname'])){
								echo '<script>alert("链接名称不能为空！"); </script>';
								header("refresh:0;url=edit.php?id={$id}");
								exit();
							}
							if(empty($_POST['links'])){
								echo '<script>alert("链接不能为空！"); </script>';
								header("refresh:0;url=edit.php?id={$id}");
								exit();
							}

	                	
	                	//拼装sql语句
	                	$sql = "update links set urlname='{$urlname}',links='{$links}'where id={$id}";
	                	// echo $sql;
	                	//执行
	                	mysqli_query($link,$sql);
	                	if(mysqli_affected_rows($link)>0){
	                		echo '<script>alert("修改成功！"); </script>';
							// header("Location:".$_SERVER["HTTP_REFERER"]);
							header("refresh:0;url=index.php");
							exit();
	                	}else{
							echo '<script>alert("没有做出修改！"); </script>';
							header("refresh:0;url=index.php");
							exit();
	                	}
	                	break;
	                	mysql_free_result($result);
						mysql_close($link);
				}
			?>
		</center>
	</body>
</html>